<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">创建时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="pTime" class="layui-input date-icon" type="text"
                                               placeholder="选择时间" style="width: 200px" readonly/>
                                    </div>
                                    <input name="startCreateTime" id="sTime" type="hidden"/>
                                    <input name="endCreateTime" id="eTime" type="hidden"/>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" layui-form-keyDownSearch="true" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="robotStationBtnAdd" class="layui-btn icon-btn color-reseda">添加
                            </button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="robotStationTable" lay-filter="robotStationTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="robotStationTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="robotStationForm">
    <form lay-filter="robotStationForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入名称" type="text" class="layui-input" lay-verify="required"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">编号</label>
                <div class="layui-input-block">
                    <input name="code" placeholder="请输入编号" type="text" class="layui-input" lay-verify="required"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-block">
                    <select name="type">
                        <option value="device">设备</option>
                        <option value="artificial">人工</option>
                        <option value="park">停泊</option>
                        <option value="avoid">避让</option>
                        <option value="charge">充电桩</option>
                        <option value="other">其他</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">所属车间</label>
                <div class="layui-input-block">
                    <select name="workshopId" id="workshopId" lay-filter="workshopId" lay-verify="required"
                            required></select>
                </div>
            </div>
        </div>
        <div class="layui-col-xs6">
            <div class="layui-form-item">
                <label class="layui-form-label">所属工序</label>
                <div class="layui-input-block">
                    <select name="workProcedureId" id="workProcedureId" lay-filter="workProcedureId"
                            lay-verify="required"
                            required></select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">责任部门</label>
                <div class="layui-input-block">
                    <select name="bigintId" id="bigintId" lay-filter="bigintId" lay-verify="required"
                            required></select>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">责任岗位</label>
                <div class="layui-input-block">
                    <select name="postId" id="postId" lay-filter="postId" lay-verify="required" required></select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">设备类型</label>
                <div class="layui-input-block">
                    <select name="deviceTypeId" id="deviceTypeId" lay-filter="deviceTypeId" lay-verify="required"
                            required></select>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">设备</label>
                <div class="layui-input-block">
                    <select name="deviceId" id="deviceId" lay-filter="deviceId" lay-verify="required"
                            required></select>
                </div>
            </div>
        </div>

        <div class="layui-form-item text-right" style="margin-top: 55px;">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="robotStationFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;

        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function (value, date, endDate) {
                if (value) {
                    var dateValue = value.split(" - ");
                    $("#sTime").val(dateValue[0] + " 00:00:00");
                    $("#eTime").val(dateValue[1] + " 23:59:59");
                } else {
                    $("#sTime").val();
                    $("#eTime").val();
                }
            }
        });
        
        // 渲染表格
        var insTb = table.render({
            elem: '#robotStationTable',
            url: config.base + 'enterprise/robot/admin/robotStation/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {field: 'number', title: '序号', type: 'numbers'}
                , {field: 'name', align: 'center', title: '名称'}
                , {field: 'code', align: 'center', title: '编号'}
                , {field: 'typeDesc', align: 'center', title: '类型'}
                , {field: 'workshopId', align: 'center', title: '所属车间'}
                , {field: 'workProcedureId', align: 'center', title: '所属工序'}
                , {field: 'postId', align: 'center', title: '责任岗位', width: 160}
                , {field: 'deviceTypeId', align: 'center', title: '设备类型', width: 160}
                , {field: 'deviceId', align: 'center', title: '设备', width: 160}
                , {field: 'creatorName', align: 'center', title: '添加人'}
                , {field: 'createTime', align: 'center', title: '添加时间', width: 160}
                , {align: 'center', toolbar: '#robotStationTableBar', title: '操作', width: 180}
            ]],
            done: function (res, curr, count) {
            }
        });

        // 工具条点击事件
        table.on('tool(robotStationTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(robotStationTable)', function (obj) {
            table.reload('robotStationTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#robotStationBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: ['780px', '480px'],
                offset: ['165px', '565px'],
                title: data ? '修改机器人工站' : '添加机器人工站',
                content: $('#robotStationForm').html(),
                success: function () {
                    if (!data) {
                        admin.req('enterprise/product/admin/processInfo/list', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择工序", "workProcedureId", 1);
                            } else {
                                layer.msg('获取工序失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                        admin.req('enterprise/system/admin/spaceCharacter/list?spaceId=3', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择车间", "workshopId", 0);
                            } else {
                                layer.msg('获取车间失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                        var selectCity = function (id) {
                            admin.req('user/admin/role/list?departmentId=' + id, {}, function (res) {
                                layer.closeAll('loading');
                                if (200 == res.code) {
                                    loadSelectHtml(res.data, "请选择岗位", "postId", 0);
                                }
                            }, 'GET');
                        }
                        admin.req('user/admin/department/list', {limit: 99999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择部门", "bigintId", 0);
                            }
                        }, 'GET', true);
                        form.on('select(bigintId)', function (data) {
                            $('#postId').empty();
                            if (data.value != '') {
                                selectCity(data.value);
                            }
                        });

                        var selectDevice = function (id) {
                            admin.req('enterprise/device/admin/device/list?deviceType=' + id, {}, function (res) {
                                layer.closeAll('loading');
                                if (200 == res.code) {
                                    loadSelectHtml(res.data, "请选择设备", "deviceId", 0);
                                }
                            }, 'GET');
                        }
                        admin.req('enterprise/device/admin/deviceType/list', {limit: 99999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择设备类型", "deviceTypeId", 0);
                            }
                        }, 'GET', true);
                        form.on('select(deviceTypeId)', function (data) {
                            $('#deviceId').empty();
                            if (data.value != '') {
                                selectDevice(data.value);
                            }
                        });
                    }
                    if (data) {
                        admin.reqSync('enterprise/product/admin/processInfo/list', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择工序", "workProcedureId", 1);
                            } else {
                                layer.msg('获取工序失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                        admin.reqSync('enterprise/system/admin/spaceCharacter/list?spaceId=3', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择车间", "workshopId", 0);
                            } else {
                                layer.msg('获取车间失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                        admin.reqSync('user/admin/department/list', {}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择部门", "bigintId", 0);
                            }
                        }, 'GET', true);

                        admin.reqSync('user/admin/role/list?departmentId=' + data.bigintId, {}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择岗位", "postId", 0);
                            }
                        }, 'GET', true);
                        admin.reqSync('enterprise/device/admin/deviceType/list', {}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择设备类型", "deviceTypeId", 0);
                            }
                        }, 'GET', true);
                        admin.reqSync('enterprise/device/admin/device/list?deviceType=' + data.deviceTypeId, {}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择设备", "deviceId", 0);
                            }
                        }, 'GET');
                    }

                    if (data) {
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function (index, item) {
                            if (data.type == item.text) {
                                data.type = item.value;
                            }
                        });
                    }

                    form.val('robotStationForm', data);
                    // 表单提交事件
                    form.on('submit(robotStationFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/robot/admin/robotStation/update' : 'enterprise/robot/admin/robotStation/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('robotStationTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/robot/admin/robotStation/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type === 0) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type === 1) {
                    html += `<option value='${data[i].id}'>${data[i].corporateName}</option>`;
                }
            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            var field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type === 0) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type === 1) {
                    html += `<option value='${data[i].id}'>${data[i].nodeNames}</option>`;
                }
            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

    });
</script>